#include "e2prom.h"

#include <stdio.h>
#include <string.h>


static void dummy_delay(volatile unsigned int t) {
	while (t --);
}


int AT24C256_example(void) {
	char buffer[] = "We come in peace!";
	int device_address_lenb = 2;
	int device_slave_address = 0xA0;
	int start_address = 0;
	int err;
	
	/* open device */
	E2PROM_open(device_address_lenb, device_slave_address);
	
	/* write to device */
	E2PROM_wp_disable();
	err = E2PROM_write(start_address, (unsigned char *)buffer, sizeof(buffer)); 
 	E2PROM_wp_enable();
	
	if(err != 0) {
		E2PROM_close();
		return -1;
	}
	
	/* delay 20ms */
	dummy_delay(200000);
	
	/* read back to check */
	if(E2PROM_read(0, (unsigned char *)buffer, sizeof(buffer)) != 0) {
	 	E2PROM_close();
		return -1;
	}
	
	/* close device */
	E2PROM_close();
	return 0;
}
